package com.example.infowall02.controller;

import com.example.infowall02.domain.Admin;
import com.example.infowall02.service.CommentService;
import com.example.infowall02.service.impl.VerificationCodeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.example.infowall02.utils.Result;
import com.example.infowall02.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.infowall02.utils.TokenUtils;

@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private CommentService commentService;
    @Resource
    private TokenUtils tokenUtils;
    @Autowired
    private VerificationCodeService verificationCodeService;

    // 登录
    @PostMapping("/login")
    public Result<Admin> loginController(// @RequestParam("adminName") String adminName, @RequestParam("adminPassword")
                                         // String adminPassword,
            @RequestBody Admin loginAdmin,
            @RequestParam(required = false) String captcha,
            @RequestParam String verificationCode,
            HttpSession session,
            HttpServletResponse response) {
        //执行验证码验证
        String expectedCaptcha = (String) session.getAttribute("kaptcha");
        session.removeAttribute("kaptcha"); // 使用后删除 session 中的验证码属性
        if (!captcha.equalsIgnoreCase(expectedCaptcha)) {
            return Result.error("图形验证码错误");
        }
        // 从请求中获取用户名和密码和邮箱
        String adminName = loginAdmin.getAdminName();
        String adminPassword = loginAdmin.getAdminPassword();
        String adminEmail = loginAdmin.getAdminEmail();
        // 执行登录验证
        Admin admin = adminService.login(adminName, adminPassword, adminEmail);
        if (admin != null) {
            // 验证邮箱验证码
            if (!verificationCodeService.verify(adminEmail, verificationCode)) {
                return Result.error("邮箱验证码错误");
            }
            // 登录成功，将用户信息存入 session 中
            session.setAttribute("loggedInAdmin", admin.getAdminName());
            // 登录成功，生成长令牌并存储在 cookie 中
            String longToken = tokenUtils.generateLongToken(admin.getAdminName());
            // Cookie 为键值对数据格式
            Cookie cookie_longToken = new Cookie("cookie_longToken", longToken);
            cookie_longToken.setMaxAge(3600 * 24 * 7); // 设置 Cookie 过期时间为 1 周
            // 表示当前项目下都携带这个cookie
            cookie_longToken.setPath("/");
            // 添加到 response 中
            response.addCookie(cookie_longToken);
            // 重要信息置空
            admin.setAdminPassword("");
            // 登录成功
            return Result.success(admin);
        } else {
            return Result.error("登录失败，用户名或密码错误或邮箱不匹配");
        }
    }

    // 退出登录
    @PostMapping(value = "/logout")
    public Result logout(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        // 删除session里面的用户信息
        session.removeAttribute("loggedInAdmin");
        // 删除cookie，实现退出登录
        Cookie cookie_longToken = new Cookie("cookie_longToken", "");
        // 设置cookie的持久化时间，0
        cookie_longToken.setMaxAge(0);
        // 设置为当前项目下都携带这个cookie
        cookie_longToken.setPath(request.getContextPath());
        // 向客户端发送cookie
        response.addCookie(cookie_longToken);
        return Result.success("退出登录成功");
    }

    // 注册
    @PostMapping("/register")
    public Result<Admin> register(@RequestBody Admin registerAdmin) {
        String adminName = registerAdmin.getAdminName();
        String adminPassword = registerAdmin.getAdminPassword();
        String adminEmail = registerAdmin.getAdminEmail();
        // 执行注册验证
        Admin admin = adminService.register(adminName, adminPassword, adminEmail);
        if (admin != null) {
            // 注册成功
            admin.setAdminPassword("");
            return Result.success(admin);
        } else {
            return Result.error("注册失败");
        }
    }
}
